package org.wheel.plugins.toolkit.sql.capability;

import cn.hutool.core.lang.Pair;
import cn.hutool.db.sql.Direction;
import org.wheel.plugins.toolkit.sql.helper.SFunction;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public interface LambdaOrder<W extends LambdaOrder<W, T>, T> {

    /* 单字段 */
    default <A, B> W orderByAsc(SFunction<A, B> col) {
        return orderByAsc(col, null);
    }
    default <A, B> W orderByAsc(SFunction<A, B> col, String tableAlias) {
        return order(Direction.ASC, col, tableAlias);
    }

    default <A, B> W orderByDesc(SFunction<A, B> col) {
        return orderByDesc(col, null);
    }
    default <A, B> W orderByDesc(SFunction<A, B> col, String tableAlias) {
        return order(Direction.DESC, col, tableAlias);
    }

    /* 多字段 */
    @SuppressWarnings("unchecked")
    default <A, B> W orderByAsc(SFunction<A, B>... cols) {
        List<Pair<SFunction<A, B>, String>> colList = Arrays.stream(cols)
                .map(c -> Pair.of(c, (String) null))
                .collect(Collectors.toList());
        return order(Direction.ASC, colList);
    }

    /* 原子方法 */
    <A, B> W order(Direction direction, SFunction<A, B> col, String tableAlias);
    <A, B> W order(Direction direction, List<Pair<SFunction<A, B>, String>> cols);
}
